package com.yanghui.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanghui.annotation.SystemLog;
import com.yanghui.pojo.Blog;
import com.yanghui.service.BlogService;
import com.yanghui.utils.BlogIdSafeUtil;
import com.yanghui.utils.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@Controller
public class BlogController {
    @Autowired
    private BlogService blogService;


    /**
     * 模糊组合分页查询博客信息(含content)
     */
    @RequestMapping(value = "/selectLikeBlogListByPage")
    @ResponseBody
    @SystemLog(description = ConstantUtil.BLOG_FIND_KEY,userType= ConstantUtil.USER_TYPE_USER)
    public Map<String, Object> selectLikeBlogListByPage(
            String param,
            Blog blog,
            @RequestParam(value="sort", required=true,defaultValue="addTime") String sort,
            @RequestParam(value="page", required=true,defaultValue="1") Integer page,
            @RequestParam(value="pageSize", required=true,defaultValue="10") Integer pageSize) throws Exception{
        Map<String, Object> map=new HashMap<>();
        map.put("sort", sort);
        if(blog.getTitle() != null && "".equals(blog.getTitle()) != true){
            map.put("title", blog.getTitle());
        }
        if(blog.getIntroduction() != null && "".equals(blog.getIntroduction()) != true){
            map.put("introduction", blog.getIntroduction());
        }
        if(blog.getKeyword() != null && "".equals(blog.getKeyword()) != true){
            map.put("keyword", blog.getKeyword());
        }
        if(blog.getContent() != null && "".equals(blog.getContent()) != true){
            map.put("content", blog.getContent());
        }
        if(blog.getIsTop() != null && "".equals(blog.getIsTop()) != true){
            map.put("isTop", blog.getIsTop());
        }
        if(blog.getTypeId() != null && "".equals(blog.getTypeId()) != true){
            map.put("typeId", blog.getTypeId());
        }

        /** 只查询已经发布的博客 */
        map.put("status", 1);

        if(blog.getStatus() != null && "".equals(blog.getStatus()) != true){
            map.put("status", blog.getStatus());
        }
        if(blog.getIsRecommend() != null && "".equals(blog.getIsRecommend()) != true){
            map.put("isRecommend", blog.getIsRecommend());
        }
        if(blog.getAddTime() != null && "".equals(blog.getAddTime()) != true){
            map.put("addTime", blog.getAddTime());
        }

        /** 使用 pageHelper 插件进行分页 */
        PageHelper.startPage(page, pageSize);
        List<Blog> blogList = blogService.selectLikeBlogListByPage(map);
        PageInfo<Blog> pageInfo = new PageInfo<>(blogList);
        Map<String,Object> returnMap = new HashMap<>();

        if(blogList.size() > 0){
            returnMap.put("status", 200);
        }else{
            returnMap.put("status", 500);
        }
        returnMap.put("blogList", blogList);
        returnMap.put("pageInfo", pageInfo);

        return returnMap;
    }


    /**
     * 查询上一篇博客
     */
    @RequestMapping("/selectPrevBlog")
    @ResponseBody
    public Map<String,Object> selectPrevBlog(Integer id) {
        Map<String,Object> map = new HashMap<>();
        Blog blog = blogService.selectPrevBlog(id);

        if (blog != null) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("blog", blog);

        return map;
    }

    /**
     * 按照 id 查询下一篇博客
     */
    @RequestMapping("/selectNextBlog")
    @ResponseBody
    public Map<String,Object> selectNextBlog(Integer id) {
        Map<String,Object> map = new HashMap<>();
        Blog blog = blogService.selectNextBlog(id);

        if (blog != null) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("blog", blog);

        return map;
    }

    /**
     * 按照 id 查询出对应的博客
     */
    @RequestMapping("/find/{id}")
    public String selectBlogById(@PathVariable(value = "id") Integer id, Model model) {
        id = BlogIdSafeUtil.BlogIdToSafe(id);

        if (id == null || id <= 0) {
            model.addAttribute("satus", 200);
        } else {
            Blog blog = blogService.selectBlogById(id);
            if (blog == null) {
                model.addAttribute("status", 500);
            } else {
                model.addAttribute("status", 200);
                /** 点击数加1 */
                blog.setClickNum(blog.getClickNum() + 1);
                blogService.updateBlog(blog);
            }
            model.addAttribute("blog", blog);
        }

        return "page/info";
    }

    /**
     * 把所有的博客类别都查询出前N篇博客
     * @return
     */
    @RequestMapping("/selectBlogByAllType")
    @ResponseBody
    public Map<String,Object> selectBlogByAllType() {
        Map<String,Object> map = new HashMap<>();
        Map<String,List<Blog>> blogMap = blogService.selectBlogByAllType();

        if (blogMap.size() > 0) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("blogMap", blogMap);

        return map;
    }

    /**
     * 按照给定的条件查询博客
     * 如标题、关键字、博客状态条件
     * 以及是否按照某个字段进行排序
     */
    @RequestMapping("/selectGroupLikeBlogListByPage")
    @ResponseBody
    public Map selectGroupLikeBlogListByPage(
            Blog blog,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sort", required = true, defaultValue = "addTime") String sort
    ) {
        /** 把要查询的条件封装到 Map 中 */
        Map<String,Object> map = new HashMap<>();
        map.put("sort", sort);
        if (blog.getTitle() != null && "".equals(blog.getTitle()) != true) {
            map.put("title", blog.getTitle());
        }
        if (blog.getKeyword() != null && "".equals(blog.getKeyword()) != true) {
            map.put("keyword", blog.getKeyword());
        }
        if (blog.getStatus() != null && "".equals(blog.getStatus()) != true) {
            map.put("status", blog.getStatus());
        }
        if (blog.getIsRecommend() != null) {
            map.put("recommend", blog.getIsRecommend());
        }
        if (blog.getIsTop() != null) {
            map.put("isTop", blog.getIsTop());
        }

        /** 使用PageHelper插件将结果进行封装分页 */
        PageHelper.startPage(page, pageSize);
        List<Blog> blogList = blogService.selectGroupLikeBlogListByPage(map);
        PageInfo pageInfo = new PageInfo(blogList);
        Map<String,Object> resultMap = new HashMap<>();

        /** 如果查询到了对应的博客，封装成功状态码 200 */
        if (blogList.size() > 0) {
            resultMap.put("status", 200);
            /** 没有查询到对应的博客，封装失败状态码 500 */
        } else {
            resultMap.put("status", 500);
        }

        resultMap.put("blogList", blogList);
        resultMap.put("pageInfo", pageInfo);

        return resultMap;
    }

}
